#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：Employment_class 
@File    ：05_模拟QQ聊天.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/7/5/005 14:51 
"""

from socket import *


def recvData(socket):
    while True:
        data = socket.recvfrom(1024)
        print("\n【%s】：【%s】" % (data[1], data[0].decode("gb2312")),end="\n")


def sendtoData(socket, addr):
    while True:
        data = input("请输入需要发送的数据：")
        socket.sendto(data.encode("gb2312"), addr)


if __name__ == '__main__':
    from threading import Thread

    udpSocket = socket(AF_INET, SOCK_DGRAM)
    udpSocket.bind(("", 7788))
    addr_ip = input("请输入要发送的IP：")
    addr_prod = input("请输入要发送的端口：")
    tr = Thread(target=recvData, args=(udpSocket,))
    tw = Thread(target=sendtoData, args=(udpSocket, (addr_ip, int(addr_prod))))
    tr.start()
    tw.start()
    tr.join()
    tw.join()


